package com.bianmaba.spring.mvc.controller;

import com.bianmaba.spring.data.jpa.domain.PageableRequest;
import com.bianmaba.spring.data.jpa.service.IBaseService;
import com.bianmaba.spring.mvc.method.support.annotations.EntityParam;
import com.bianmaba.spring.mvc.method.support.annotations.JSON;
import com.bianmaba.spring.mvc.method.support.annotations.MapParam;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

public abstract class ReadController<T, ID extends Serializable> extends ControllerSupports {

    protected IBaseService<T, ID> getService() {
        return null;
    }


    @JSON()
    @RequestMapping(value = "/get_by_id")
    public T getById(ID id) {
        T result = getService().findOne(id);
        return result;
    }

    @JSON()
    @RequestMapping(value = "/all")
    public List<T> all() {
        return getService().query();
    }

    @JSON()
    @RequestMapping(value = "/query")
    public Page<T> query(@MapParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        return getService().query(params, pageable);
    }

    @JSON()
    @RequestMapping(value = "/query_distinct")
    public Page<T> queryDistinct(@MapParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        return getService().queryDistinct(params, pageable);
    }
}
